$("#form-customerInfo-add").validate({
	rules:{
		name:{
			required:true,
		},
		cellphone:{
			required:true,
			remote : {
				url : ctx + "module/customerInfo/checkCellphone",
				type : "post",
				dataType : "json",
				data : {
					name : function() {
						return $.trim($("#cellphone").val());
					}
				},
				dataFilter : function(data, type) {
					if (data == "0")
						return true;
					else
						return false;
				}
			}
		},
		age:{
			required:true,
		},
		identityNum:{
			required:true,
		},
	},
	submitHandler:function(form){
		
		add();
	}
});


function add() {
/*	if($("select[name='province'] option:selected").text()=='--请选择--'){
		$("select[name='province'] option:selected").text('-');
		debugger
	}
	if($("select[name='city'] option:selected").text()=='--请选择--'){
		$("select[name='city'] option:selected").text('');
		console.log($("select[name='city'] option:selected").text())
		debugger
	}
	if($("select[name='area'] option:selected").text()=='--请选择--'){
		$("select[name='area'] option:selected").text('');
		debugger
	}*/
	var name = $("input[name='name']").val();
	var cellphone = $("input[name='cellphone']").val();
	var age = $("input[name='age']").val();
	var sex = $("input[name='sex']:checked").val();
	var backgroundId = $("select[name='backgroundId']").val();
	var backgroundName = $("select[name='backgroundId'] option:selected").text();
	var identityNum = $("input[name='identityNum']").val();
	var createBy = $("input[name='createBy']").val();
	var job =$("select[name='job'] option:selected").text();
	var education =$("select[name='education'] option:selected").text();
	var birth = $("input[name='birth']").val();	
	var phoneNum = $("input[name='phoneNum']").val();
	var province = $("select[name='province'] option:selected").text();
	var city = $("select[name='city'] option:selected").text();
	var area = $("select[name='area'] option:selected").text();
	var addressDetail = $("input[name='addressDetail']").val();
	if(province=='--请选择--'){
		province='';
		console.log(province)
	}
	if(city=='--请选择--'){
		city='';
		console.log(city)
	}
	if(area=='--请选择--'){
		area='';
	}
   
	var myreg=/^[1][3,4,5,7,8][0-9]{9}$/; //手机号码
	var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/; //身份证号码 
	if(!myreg.test(cellphone)){
		layer.msg('请输入正确的手机号码')
		return false
	}else if(!reg.test(identityNum)){
		layer.msg('请输入正确的身份证号码')
		return false
	}else{
		$.ajax({
			cache : true,
			type : "POST",
			url : ctx + "module/customerInfo/save",
			data : {
				"name" : name,
				"cellphone" : cellphone,
				"age" : age,
				"sex" :sex,
				"backgroundId" :backgroundId,
				"backgroundName" : backgroundName,
				"identityNum" : identityNum,
				"job" : job,
	            "education" : education,
				"createBy" : createBy,
				"birth" : birth,
		        "phoneNum" : phoneNum,
		        "province" : province,
		        "city" : city,
		        "area" : area,
		        "addressDetail" : addressDetail,
			},
			async : false,
			error : function(request) {
				$.modalAlert("系统错误", "error");
			},
			success : function(data) {
				if (data.code == 0) {
					parent.layer.msg("新增成功,正在刷新数据请稍后……",{icon:1,time: 500,shade: [0.1,'#fff']},function(){
						$.parentReload();
					});
				} else {
					$.modalAlert(data.msg, "error");
				}

			}
		});
	}
}

$(function() {
	var html = "";
	$("#input_city").append(html);
	$("#input_area").append(html);
	$.each(pdata, function(idx, item) {
		if (parseInt(item.level) == 0) {
			html += "<option value='" + item.names + "' exid='" + item.code
					+ "'>" + item.names + "</option>";
		}
	});
	$("#input_province").append(html);
	$("#input_province").change(
			function() {
				if ($(this).val() == "")
					return;
				$("#input_city option").remove();
				$("#input_area option").remove();
				var code = $(this).find("option:selected").attr("exid");
				code = code.substring(0, 2);
				var html = "<option value=''>--请选择--</option>";
				$("#input_area").append(html);
				$.each(pdata, function(idx, item) {
					if (parseInt(item.level) == 1
							&& code == item.code.substring(0, 2)) {
						html += "<option value='" + item.names + "' exid='"
								+ item.code + "'>" + item.names + "</option>";
					}
				});
				$("#input_city").append(html);
			});
	$("#input_city").change(
			function() {
				if ($(this).val() == "")
					return;
				$("#input_area option").remove();
				var code = $(this).find("option:selected").attr("exid");
				code = code.substring(0, 4);
				var html = "<option value=''>--请选择--</option>";
				$.each(pdata, function(idx, item) {
					if (parseInt(item.level) == 2
							&& code == item.code.substring(0, 4)) {
						html += "<option value='" + item.names + "' exid='"
								+ item.code + "'>" + item.names + "</option>";
					}
				});
				$("#input_area").append(html);
			});
});